﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WSPay.Common;
using WSPay.Application;
using WSPay.Core.DTO;
using WSPay.Domain;
using WSPay.Web.Models;

namespace WSPay.Web.Controllers
{
    public class APIController : BaseController
    {
        public IWSBService WSBService
        {
            get; private set;
        }

        public APIController(IWSBService wsbService)
        {
            WSBService = wsbService;
        }


        public ActionResult TestWSBPay(WSBPayDTO pay)
        {
            return View(pay);
        }
        
        
        [HttpPost]
        public JsonResult WSBPay(WSBPayDTO pay)
        {
            try
            {

                var result = WSBService.WSBTransactions(pay);

                return Json(result, JsonRequestBehavior.DenyGet);
            }catch(Exception ex) { 
                
                return Json(ex.Message, JsonRequestBehavior.DenyGet);
            }
        }

        public JsonResult WSBAccount(Guid userID,string userName="") {
            var result = WSBService.QueryWsbAccount(userID, userName);
            return Json(result,JsonRequestBehavior.AllowGet);
        }

        public JsonResult WSBAccountSeq(Guid userID, int page, int pageSize = 20, string startTime = "", string endTime = "", string type = "")
        {
            IOrderedQueryable<WSBAccountSeq> orderedQueryable =
                from x in this.WSBService.QueryWSBAccountSeq()
                where x.UserID == userID
                orderby x.CreateTime descending
                select x;
            if (!string.IsNullOrWhiteSpace(startTime) && !string.IsNullOrWhiteSpace(endTime))
            {
                DateTime start = DateTime.Parse(startTime);
                DateTime end = DateTime.Parse(endTime);
                orderedQueryable =
                    from x in orderedQueryable
                    where x.CreateTime >= start && x.CreateTime <= end
                    orderby x.CreateTime descending
                    select x;
            }
            if (!string.IsNullOrWhiteSpace(type))
            {
                ChangeType flag = (ChangeType)Enum.Parse(typeof(ChangeType), type);
                orderedQueryable =
                    from x in orderedQueryable
                    where (int)x.ChangeType == (int)flag
                    orderby x.CreateTime descending
                    select x;
            }
            decimal incomCount = 0m;
            decimal outCount = 0m;
            if (orderedQueryable.Any<WSBAccountSeq>())
            {
                IQueryable<WSBAccountSeq> source =
                    from x in orderedQueryable
                    where (int)x.FlowFlag == (int)0
                    select x;
                if (source.Any<WSBAccountSeq>())
                {
                    incomCount = source.Sum((WSBAccountSeq x) => x.CashAmount);
                }
                IQueryable<WSBAccountSeq> source2 =
                    from x in orderedQueryable
                    where (int)x.FlowFlag == (int)1
                    select x;
                if (source2.Any<WSBAccountSeq>())
                {
                    outCount = source2.Sum((WSBAccountSeq x) => x.CashAmount);
                }
            }
            return base.SuccessPage(PageListExtensions.ToPagedList<WSBAccountSeq>(orderedQueryable, page, pageSize), new PageList
            {
                PageIndex = page,
                TotalCount = orderedQueryable.Count<WSBAccountSeq>(),
                IncomCount = incomCount,
                OutCount = outCount
            });
        }
    }
}